import BaseChartOption from '../model/chartoption.js'


export {
    setdataAll01,
    getMax,
    getDoubleMax
}

function getDoubleMax(array) {
    let maxs = [
        [],
        []
    ];
    for (let i = 0; i < array.length; i++) {
        for (let j = 0; j < array[i].length; j++) {
            maxs = maxs.concat(array[i][j]);
        }

    }
    return Math.max.apply(Math, maxs);
}

function getMax(array) {
    let maxs = [];
    for (let i = 0; i < array.length; i++) {
        maxs = maxs.concat(array[i]);
    }
    return Math.max.apply(Math, maxs);
}

/**
 * @param {Array} x轴的名称
 * @param {Array} [{name:'yyy',type:'bar',data:[40,6,25],yAxisIndex?: 0}]x轴的值
 * @param {string} 标题
 * @param {Array} [{type?:'value',name?:'yaix',min?:0,max?:0,formatter:'%'}]注意其中的max的值默认100；
 */

function setdataAll01(datax, datas, title, yconfig) { //datax x轴[data],y轴配置 datas数据[{name:'',type:'line'?'bar',[data]}]

    let optiondata = JSON.parse(JSON.stringify((BaseChartOption.dataAll.dataAll01)));
    if (title)
        optiondata.title.text = title;
    if (datax)
        optiondata.xAxis.data = datax;
    if (yconfig) {
        optiondata.yAxis = [];
        for (var i = 0; i < yconfig.length; i++) {
            if (i === 0) {
                optiondata.yAxis[i] = {
                    type: yconfig[i].type ? yconfig[i].type : 'value',
                    name: yconfig[i].name ? yconfig[i].name : '',
                    min: yconfig[i].min ? yconfig[i].min : 0,
                    max: yconfig[i].max ? yconfig[i].max : 100,
                    axisLabel: {
                        formatter: yconfig[i].formatter ? '{value} ' + yconfig[i].formatter : '{value}'
                    }
                }
            } else if (i === 1) {
                optiondata.yAxis[i] = {
                    type: yconfig[i].type ? yconfig[i].type : 'value',
                    name: yconfig[i].name,
                    min: yconfig[i].min ? yconfig[i].min : 0,
                    max: yconfig[i].max ? yconfig[i].max : 100,
                    axisLabel: {
                        formatter: yconfig[i].formatter ? '{value} ' + yconfig[i].formatter : '{value}'
                    }
                }
            }
        }
    }
    if (datas) {
        for (var i = 0; i < datas.length; i++) {
            optiondata.series[i] = datas[i];
            optiondata.legend.data[i] = datas[i].name;
        }
    }
    return optiondata;
}